#!/bin/env  python
# -*- coding: gbk -*- 
import sys

path = "/home/spider/atscript/lib/"
sys.path.append(path)

from atscript import *
from hadoop import *
from assert_func import *

conf = config("rankget.yaml")
sample = base(conf.get("CONF"), conf.get("SCHEDULE"), conf.get("JOBLIST"))
hadoop = hadoop(conf,"/home/spider/rankget/conf/hadoop-site.xml")

class Test_rg_downloadclick(at_case):
    def __init__(self, casename, base, jobname, input, output):
        at_case.__init__(self, casename, base, jobname, input, output)
    
    def set_input(self):
        line1 = "click\t274\t304\t48\thttp://www.test.com/click_http\t150\thttps://www.test.com/click_https\t40\thwww.test.com/click\n"
        line2 = "URL: 2011-07-02 23:59:59: 12ÔÂ3ÈÕ       b15cbc770005a274:1:10:0:745587523::baidujp:1:1577065:760:12ÔÂ3ÈÕ°kÓ-2009Äê12ÔÂ3ÈÕ-Æ½³É15Äê12ÔÂ3ÈÕ-12ÔÂ3ÈÕ¥ª©`¥×¥ó-12ÔÂ3ÈÕÉú¤Þ¤ì-2007Äê12ÔÂ3ÈÕ-2008Äê12ÔÂ3ÈÕ::/s?wd=12%E6%9C%883%E6%97%A5&ie=utf-8:1 http://www.test.com/dispaly_http  2:AS:268436916:4513972:9981806:141772552:13089708:2:2:813685756:17956867:1048576-0-0-0:131072:::::0:0  https://www.test.com/dispaly_https  3:AS:268436916:4513972:9981806:141772552:13089708:2:2:813685756:17956867:104857 6-0-0-0:131072:::::0:0 www.test.com/dispaly  4:AS:268436916:4513972:9981806:141772552:13089708:2:2:813685756:17956867:104857  6-0-0-0:131072:::::0:0\n" 
        temp_input_file1 = conf.get("MODULE_HOME")+"data/"+self.casename + ".click_input"
        temp_input_file2 = conf.get("MODULE_HOME")+"data/"+self.casename + ".display_input"

        try:
            file_handle1 = open(temp_input_file1, 'w')
            file_handle2 = open(temp_input_file2, 'w')
            file_handle1.write(line1)
            file_handle2.write(line2)
        except IOError, e:
            raise IOError
        finally:
            if file_handle1:
                file_handle1.close()
            if file_handle2:
                file_handle2.close()

        file_handle = open("downloadclick.list",'w')
        file_handle.write("0 ftp://bb-testing-ps70233.vm" + temp_input_file1 + "\n")
        file_handle.write("1 ftp://bb-testing-ps70233.vm" + temp_input_file2 + "\n")
        file_handle.close()

        hadoop.rmfile(self.input + "downloadclick.list")
        hadoop.put("downloadclick.list", self.input)
    
    def run(self):
        hadoop.rmdir(self.output)
        hadoop.mkdir(self.output)
        
        print self.run_cmd
        self.execute_cmd(self.run_cmd, True)

    def check_result(self):
        expect_out = self.casename + ".expect_out"
        real_out = self.casename + ".real_out"

        self.execute_cmd("rm -f %s" % real_out)        
        hadoop.getmerge(self.output, real_out)
          
        try:
            file_handle = open(expect_out, 'w')
            file_handle.write("www.test.com/click_http\tCLICK_AND_ZHANXIAN\n")
            file_handle.write("www.test.com/click_https\tCLICK_AND_ZHANXIAN\n")
            file_handle.write("www.test.com/dispaly_http\tCLICK_AND_ZHANXIAN\n")
            file_handle.write("www.test.com/dispaly_https\tCLICK_AND_ZHANXIAN\n")
        except IOError, e:
            raise IOError
        finally:
            file_handle.close()
        
        try:
           assert_file_equal(expect_out, real_out)        
        except AssertionError, e:
           print >>sys.stdout, "case %s failed" % self.casename
           print >>sys.stdout, "%s not equal to %s" % (real_out, expect_out)
           sys.exit(1)
        print >>sys.stdout, "case %s passed" % self.casename

if __name__ == "__main__":
     test = Test_rg_downloadclick('Test_rg_downloadclick', sample, 'rg_downloadclick', '/user/spider/rankget/input/', '/user/spider/rankget/data/downloadclick/')
     test.set_input()
     test.run()
     test.check_result()  
